// const host = 'http://192.168.2.190:11000';
import WebStorageCahe from 'web-storage-cache';
import { Message } from '@alifd/next';


const wsCache = new WebStorageCahe();

const host = process.env.NODE_ENV === 'development' ? 'http://192.168.3.98:11000' : 'http://127.0.0.1:11000';
const comEntity = null;


const defaultHeader = function () {
    const initHeaders = new Headers();
    initHeaders.append('Content-Type', 'application/json');
    return initHeaders;
};


function stringCheck(str) {
    if (str == null) return null;
    return str.trim().length > 0 ? str.trim() : null;
}


/**
 * 校验是否选择模式
 * @param {*} str
 */
function isSelectedMode() {
    let selected = true;
    const lProtocol = wsCache.get('protocol');
    if (!lProtocol) {
        Message.error('请设置通信模式!');
        selected = false;
    }
    return selected;
}
/**
 * 校验天线是否连接
 * @param {*} str
 */
function isSelectedCom() {
    let selected = true;
    const lProtocol = wsCache.get('protocol');
    if (lProtocol === MODE_COM) {
        if (window.comEntity == null || window.comEntity.status == 1) {
            Message.error('天线未初始化或未连接 请检查天线配置');
            selected = false;
            return;
        }
    }

    return selected;
}

Date.prototype.Format = function (fmt) {
    const o = {
        'M+': this.getMonth() + 1, // 月份
        'd+': this.getDate(), // 日
        'H+': this.getHours(), // 小时
        'm+': this.getMinutes(), // 分
        's+': this.getSeconds(), // 秒
        'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
        S: this.getMilliseconds(), // 毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (`${this.getFullYear()}`).substr(4 - RegExp.$1.length));
    for (const k in o) { if (new RegExp(`(${k})`).test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((`00${o[k]}`).substr((`${o[k]}`).length))); }
    return fmt;
};


function deepClone(obj) {
    let _obj = JSON.stringify(obj),
        objClone = JSON.parse(_obj);
    return objClone;
}




export const TEAM_RED = 'red';// 红队
export const TEAM_BLUE = 'blue';// 蓝队
export const TEAM_ORANGE = 'orange';// 劫匪
export const TEAM_YELLOW = 'yellow';// 人质

export const MODE_MQTT = '2';// MQTT模式
export const MODE_COM = '1';// 串口模式

export { defaultHeader, host, stringCheck, deepClone, comEntity, isSelectedMode, isSelectedCom };
